<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>猜数字游戏（2）</title>
</head>

<body>
    <script>
        // 用户有5次机会
        function guessNumber() {
            var randon_num = Math.ceil(Math.random() * 100);
            console.log(randon_num);

            var count = 5;

            var finish = false;  // finish: 完成
            while (!finish) {      // !finish 为 true  -->  ! : 表示取向反的
                count--;         // 每循环一次，就说明用户消耗了一次机会。
                var guess_num = prompt("请输入您猜的数字:(0~100)");
                console.log(guess_num);
                if (count == 0) {
                    if (randon_num > guess_num) {
                        alert("您猜小了,机会用完了 ");
                    }
                    else if (randon_num < guess_num) {
                        alert("您猜大了,机会用完了");
                    }
                    else {
                        alert("您猜对了");
                    }
                    break;   //  --> finish = true;
                }
                else {
                    if (randon_num > guess_num) {
                        alert("您猜小了,还有: " + count + "次机会");
                    }
                    else if (randon_num < guess_num) {
                        alert("您猜大了,还有: " + count + "次机会");
                    }
                    else {
                        alert("您猜对了");
                        break;   // --> finish = true;
                    }
                }

            }
        }
        guessNumber();
    </script>

</body>

</html>